﻿Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Windows.Forms
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
<Serializable()> Public Class InsertImage
    Private m_FileName As String
    Private m_Point1 As Point
    Private m_Point2 As Point

    Private m_PointReal1 As Point
    Private m_PointReal2 As Point


    Private m_LeftBottomPoint As Point '图形的左下角坐标
    Private m_RightTopPoint As Point '图形的右上角坐标

    Public BackRect As New Rectangle() '背景图的矩形。放缩后的矩形
    Public ImageRect As New Rectangle() '图形的大小和位置
    Public imageFileName As String '背景图的文件名
    Private bmp As Bitmap '底图
    Public gScale As Single = 1 '绘图比例，由于采用象素绘图，还应将比例换为mm。
    Public OriginX As Integer = 225
    Public OriginY As Integer = 225
    Public BackOriginX As Integer = 10
    Public BackOriginY As Integer = -10
    Public BackgScale As Single = 1 '默认值为1，不放缩。
    Private m_Symbol1 As New Symbol
    Private m_Symbol2 As New Symbol

    ''' <summary>
    ''' 文件名，不包含扩展名
    ''' </summary>
    ''' <remarks></remarks>
    Private m_FileNameNoExen As String

    ''' <summary>
    ''' 背景图的文件名。包含路径。
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property FileName() As String
        Get
            Return Me.m_FileName
        End Get
        Set(ByVal value As String)
            Me.m_FileName = value
        End Set
    End Property

    Property Point1() As Point
        Get
            Return Me.m_Point1
        End Get
        Set(ByVal value As Point)
            Me.m_Point1 = value
        End Set
    End Property
    Property Point2() As Point
        Get
            Return Me.m_Point2
        End Get
        Set(ByVal value As Point)
            Me.m_Point2 = value
        End Set
    End Property
    ''' <summary>
    ''' 图形的左下角坐标
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property LeftBottomPoint() As Point
        Get
            Return Me.m_LeftBottomPoint
        End Get
        Set(ByVal value As Point)
            Me.m_LeftBottomPoint = value
        End Set
    End Property

    ''' <summary>
    ''' 图形的右上角坐标
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property RightTopPoint() As Point
        Get
            Return Me.m_RightTopPoint
        End Get
        Set(ByVal value As Point)
            Me.m_RightTopPoint = value
        End Set
    End Property
    Property Symbol1() As Symbol
        Get
            Return Me.m_Symbol1
        End Get
        Set(ByVal value As Symbol)
            Me.m_Symbol1 = value
        End Set
    End Property
    Property Symbol2() As Symbol
        Get
            Return Me.m_Symbol2
        End Get
        Set(ByVal value As Symbol)
            Me.m_Symbol2 = value
        End Set
    End Property
    Property bitmap() As Bitmap '底图
        Get
            Return Me.bmp
        End Get
        Set(ByVal value As Bitmap)
            Me.bmp = value
        End Set
    End Property
    ''' <summary>
    ''' 用于保存第一个点的实际坐标
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property PointReal1() As Point
        Get
            Return Me.m_PointReal1
        End Get
        Set(ByVal value As Point)
            Me.m_PointReal1 = value
        End Set
    End Property
    ''' <summary>
    ''' 用于保存第二个点的实际坐标
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property PointReal2() As Point
        Get
            Return Me.m_PointReal2
        End Get
        Set(ByVal value As Point)
            Me.m_PointReal2 = value
        End Set
    End Property
    ''' <summary>
    ''' 文件名，不包含扩展名
    ''' </summary>
    ''' <remarks></remarks>
    Property FileNameNoExen() As String
        Get
            Return Me.m_FileNameNoExen
        End Get
        Set(ByVal value As String)
            Me.m_FileNameNoExen = value
        End Set
    End Property

    Public Sub LoadBitmap(ByVal imageFile As String)
        imageFileName = imageFile
        bmp = New Bitmap(imageFileName)
        ImageRect.Width = bmp.Width
        ImageRect.Height = bmp.Height
    End Sub
    Public Sub UnloadBitmap()
        If bmp IsNot Nothing Then
            bmp.Dispose()
            bmp = Nothing
        End If
    End Sub
    Public Sub DrawImage(ByVal grap As Graphics)
        If bmp IsNot Nothing Then
            '坐标变换
            grap.TranslateTransform(OriginX, OriginY, MatrixOrder.Append) '将坐标系统平移
            Dim graphicsContainer As GraphicsContainer
            graphicsContainer = grap.BeginContainer() '绘图容器
            Dim myMatrix As New Matrix(1, 0, 0, -1, 0, 0) '垂直翻转变换
            myMatrix.Scale(1 / gScale, 1 / gScale) '比例尺为
            grap.Transform = myMatrix
            DrawBack(grap)
            grap.EndContainer(graphicsContainer)
        End If
    End Sub
    Private Sub DrawBack(ByVal grap As Graphics)
        If bmp IsNot Nothing Then
            BackRect.X = -ImageRect.Width / 2
            BackRect.Y = -ImageRect.Height / 2
            BackRect.Width = ImageRect.Width
            BackRect.Height = ImageRect.Height
            Dim destinationPoints As Point() = { _
                                     New Point(BackRect.X, BackRect.Y + BackRect.Height), _
                                     New Point(BackRect.X + BackRect.Width, BackRect.Y + BackRect.Height), _
                                     New Point(BackRect.X, BackRect.Y)}
            grap.DrawImage(bmp, destinationPoints)
        End If

        'grap.FillRectangle(Brushes.Red, New Rectangle(P1.X - 5 * gScale, P1.Y - 5 * gScale, 10 * gScale, 10 * gScale))
        'grap.FillRectangle(Brushes.Red, New Rectangle(P2.X - 5 * gScale, P2.Y - 5 * gScale, 10 * gScale, 10 * gScale))

        Me.m_Symbol1.DrawSymbol(grap, gScale)
        Me.m_Symbol2.DrawSymbol(grap, gScale)
    End Sub

    Public Function ScreenToDraw(ByVal point As Point) As Point
        Dim x As Double = (point.X - OriginX) * gScale
        Dim y As Double = (OriginY - point.Y) * gScale
        Dim p As New Point(x, y)
        Return p
    End Function

    Public Function PointToLocation(ByVal point As PointF) As PointF
        point.X = point.X - OriginX
        point.Y = point.Y - OriginY
        point.Y = -point.Y
        point.X = point.X * gScale
        point.Y = point.Y * gScale
        Return point
    End Function

    ''' <summary>
    ''' 获得文件的名称，不包含路径和扩展名
    ''' </summary>
    ''' <param name="sName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetFileName(ByRef sName As String) As String
        Dim FileName1 As String
        FileName1 = My.Computer.FileSystem.GetName(sName)
        FileName1 = System.IO.Path.ChangeExtension(FileName1, "")
        If FileName1.EndsWith(".") Then
            FileName1 = FileName1.Substring(0, FileName1.Length - 1)
        End If
        Return FileName1
    End Function

    ''' <summary>
    ''' 反回副本
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Clone() As InsertImage
        Try
            ' Create a filestream object
            Dim fileStr As Stream = File.Open(ApplicationPath() & "InsertImage.sav", FileMode.Create)
            ' Create a linked list object and populate it with random nodes
            Dim ObjInsertImage As New InsertImage
            ObjInsertImage = Me
            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter
            formatter = CType(New BinaryFormatter, IFormatter)
            ' Serialize the object graph to stream
            formatter.Serialize(fileStr, ObjInsertImage)
            ' All done
            fileStr.Close()
        Catch ex As Exception
            MsgBox("复制背景图错误!")
            Return Nothing
        End Try


        Try
            Dim fileStr As Stream = File.Open(ApplicationPath() & "InsertImage.sav", FileMode.Open)

            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter

            formatter = CType(New BinaryFormatter, IFormatter)
            ' Deserialize the object graph from stream
            Dim ObjInsertImage As New InsertImage
            ObjInsertImage = CType(formatter.Deserialize(fileStr), InsertImage)
            fileStr.Close()
            Return ObjInsertImage
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class


